<?php


namespace app\admin\controller;
use think\Db;

/**
 * @Class User
 * @User:zhang
 */
class Teacher extends \think\Controller
{
    /**
     * @return mixed
      *@description 查询所有教室用户
     */
    public function index(){

        $rus=Db::table('zx_user')->where('grade',1)->select();
//       dump($rus);exit;
        $this->assign('rus', $rus);
//        $this->assign('pid', $pid);
        return $this->fetch('index');
    }



    /**

     *@description 到新增页
     */
    public function teacher_add_a(){

        $rus=Db::table('zx_user')->select();
//       dump($rus);exit;
        $this->assign('rus', $rus);
//        $this->assign('pid', $pid);
        return $this->fetch('add');
    }
    /**

     *@description 新增用户
     */
    public function teacher_add_bt(){
        $file = request()->file('icon');
        $tel=input('tel');
        $name=input('name');
        if (empty($tel&&$name&&$file)) {
            $this->error('请把数据填写完整');
        }

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息


            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $icon=$info->getSaveName();
//            dump( $icon);exit;
            $data=['user_name'=>$name,'tel'=>$tel,'user_img'=>$icon,'sign'=>input('sign'),'address'=>input('address'),'sex'=>input('sex'),'grade'=>1];
//            dump( $data);exit;
            $rus=db('user')->insert($data);
            if ($rus>0) {
                return $this->success('添加成功','admin/teacher/index');
            }else{
                return $this->error('失败');
            }
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
//        dump($file);exit;
//       dump(input());exit;

    }


    /**

     *@description 修改用户页面
     */
    public function teacher_edit_a(){
        $teacher_id=input('teacher_id');
        $rus=Db::table('zx_user')->where('id',$teacher_id)->find();
//       dump($rus);exit;
        $this->assign('rus', $rus);

//        $this->assign('pid', $pid);
        return view('edit');
    }


    /**

     *@description 修改用户
     */
    public function teacher_edit_bt(){
//        dump(input());exit;
        $res=db('user')->where('id',input('user_id'))
            ->update([
                'user_name' =>input('name'),
                'tel' =>input('tel'),
                'sign' =>input('sign'),
                'address' =>input('address'),
                'sex' =>input('sex'),
            ]);
        if(!$res){
            return $this->error('失败');
        }else{
            return $this->success('修改成功','admin/teacher/index');
        }
    }


    /**

     *@description 删除用户
     * @param $teacher_id 用户传的id
     */
    public function teacher_del_a(){
        $teacher_id=input('teacher_id');


            $rus=Db::table('zx_user')->where('id', $teacher_id)->delete();
            if($rus){
                //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
                $this->success('删除成功', 'teacher/index');
            } else {
                //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('删除失败');
            }


    }



    /**

     *@description 用户详情页
     */
    public function teacher_detail()
    {
        $teacher_id = input('teacher_id');


        $rus = Db::table('zx_user')->where('id', $teacher_id)->find();

        $friend_count = Db::table('zx_friend')->where('teacher_id', $teacher_id)->count();
        $shoppingcar_count = Db::table('zx_shoppingcar')->where('teacher_id', $teacher_id)->count();
        $details_count = Db::table('zx_details')->where('teacher_id', $teacher_id)->count();

//        dump( $details_count);exit;

        if ($rus) {
            $this->assign('rus', $rus);
            $this->assign('friend_count', $friend_count);
            $this->assign('details_count', $details_count);
            $this->assign('shoppingcar_count', $shoppingcar_count);
            return view('detail');
        } else {
            //错误页面的默认跳转页面是返回前一页，通常不需要设置
            $this->error('可能遇到一些问题');
        }

    }

    /**
     * @description ajax 改变用户状态
     */
    public function teacher_status_ajax(){
        $status=input('status');

        $teacher_id=input('teacher_id');
        if($status==0){
            $rus=db('user')->where("id=$teacher_id")->update(['status'=>1]);
            if($rus){
                return json(1);
            }
        }else{


            $rus=db('user')->where("id=$teacher_id")->update(['status'=>0]);
            if($rus){
                return json(0);
            }
        }



    }
}